package com.campus.service.impl;

import com.campus.constant.MessageConstant;
import com.campus.exception.AccountNotFoundException;
import com.campus.exception.AgeIllegalException;
import com.campus.exception.CaptchaExpiredException;
import com.campus.exception.PasswordErrorException;
import com.campus.mapper.CollageMapper;
import com.campus.mapper.StudentMapper;
import com.campus.pojo.dto.StudentDTO;
import com.campus.pojo.dto.StudentLoginDTO;
import com.campus.pojo.dto.StudentPageQueryDTO;
import com.campus.pojo.po.Faculty;
import com.campus.pojo.po.Student;
import com.campus.pojo.vo.StudentVO;
import com.campus.result.PageResult;
import com.campus.service.StudentService;
import com.campus.utils.StudentUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {


    private final StudentMapper studentMapper;
    private final CollageMapper collageMapper;
    private final RedisTemplate redisTemplate;

    @Override
    public StudentVO getById(Long id) {
        Student student = Student.builder()
                .id(id)
                .build();
        List<StudentVO> list = studentMapper.list(student);
        if (list.isEmpty()){
            throw new RuntimeException("查无此人");
        }
        return list.get(0);
    }

    /**
     * 分页查询学生信息
     * @param studentPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(StudentPageQueryDTO studentPageQueryDTO) {
        PageHelper.startPage(studentPageQueryDTO.getPage(), studentPageQueryDTO.getPageSize());
        Page<StudentVO> page = studentMapper.page(studentPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除学生信息
     * @param id
     */
    @Override
    public void delById(Long id) {
        studentMapper.delById(id);
    }

    /**
     * 学生数据更新
     * @param studentDTO
     */
    @Override
    public void update(StudentDTO studentDTO) throws AgeIllegalException {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);
        if (student.getAge() != null) {
            if(student.getAge() > 150 || student.getAge() < 0){
                throw new AgeIllegalException("年龄不合法");
            }
        }
        studentMapper.update(student);
    }

    /**
     * 新增学生信息
     * @param studentDTO
     */
    @Override
    @Transactional
    public void addStudent(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);
        Faculty faculty = collageMapper.getInfoByClassId(student.getClassId());
        student.setFacultyId(faculty.getId());
        // 插入学生数据 返回学生id
        studentMapper.insert(student);
        // 生成学号
        student.setSno(StudentUtil.sno(student));
        studentMapper.update(student);
    }

    @Override
    public Student login(StudentLoginDTO studentLoginDTO) {
        //根据学号查询学生信息
        Student student = studentMapper.getBySno(studentLoginDTO.getSno());
        // 获取redis缓存的验证码
        String captcha = (String) redisTemplate.opsForValue().get(studentLoginDTO.getKey());
        if(captcha == null){
            // 验证码失效
            throw new CaptchaExpiredException(MessageConstant.CAPTCHA_EXPIRED);
        }
        if(!captcha.equals(studentLoginDTO.getCaptcha())){
            // 验证码错误
            throw new CaptchaExpiredException(MessageConstant.CAPTCHA_ERROR);
        }
        if(student == null){
            //学生信息不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if(!student.getPassword().equals(studentLoginDTO.getPassword())){
            // 密码错误
            throw new PasswordErrorException(MessageConstant.ACCOUNT_PASSWORD_ERROR);
        }

        return student;
    }
}
